package com.mlnx.work.service.iml;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mlnx.common.form.PageForm;
import com.mlnx.work.dao.mysql.ProjectDirMapper;
import com.mlnx.work.entity.ProjectDir;
import com.mlnx.work.entity.Right;
import com.mlnx.work.entity.User;
import com.mlnx.work.enums.ResponseEnum;
import com.mlnx.work.exception.WorkServiceException;
import com.mlnx.work.pojo.dto.DateDTO;
import com.mlnx.work.pojo.form.FilterForm;
import com.mlnx.work.pojo.form.ProjectFilterForm;
import com.mlnx.work.pojo.vo.*;
import com.mlnx.work.service.ProjectDirService;
import com.mlnx.work.service.RightService;
import com.mlnx.work.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * author name: amanda.shan
 * create time: 2020-12-28 16:58:01
 */
@Service
public class ProjectDirServiceIml extends BaseService<ProjectDirMapper, ProjectDir> implements ProjectDirService {

    private Logger logger = LoggerFactory.getLogger(ProjectDirServiceIml.class);

    @Autowired
    @Override
    public void setBaseMapper(ProjectDirMapper baseMapper) {
        super.setBaseMapper(baseMapper);
    }

    @Autowired
    private UserService userService;
    @Autowired
    private RightService rightService;

    @Override
    public List<MemberVO> getMemberById(Integer dirId) {
        return baseMapper.selectMemberById(dirId);
    }

    @Override
    public List<ProjectListVO> getProjectListByMember(Integer memberId) {
        User user = userService.getById(memberId);
        List<ProjectListVO> projectListVOList;
        List<Right> rightsByUserName = rightService.getRightsByUserName(getUsername());
        for (Right right : rightsByUserName) {
            if ("right_view_all_project".equals(right.getRightKey())) {
                projectListVOList = baseMapper.selectAllProject();
                return projectListVOList;
            }

            if ("right_view_dept_project".equals(right.getRightKey())) {
                projectListVOList = baseMapper.selectProjectByDept(user.getDeptId());
                return projectListVOList;
            }
        }

        return baseMapper.selectProjectByMember(memberId);
    }

    @Override
    public ProjectVO getProjectById(Integer dirId) {
        ProjectVO projectVO = baseMapper.selectProjectById(dirId);
        String rootNo = "1";
        projectVO.setItemNo(rootNo);
        createItemNo(rootNo, projectVO.getChildProjectVO());

        return projectVO;
    }

    @Override
    public ProjectDetailVO getProjectDetailById(Integer dirId) {
        return baseMapper.selectProjectDetailById(dirId);
    }

    @Override
    public Page<ProjectTaskVO> getProjectTask(Long startTime, Long endTime, PageForm pageForm, ProjectFilterForm projectFilterForm) {
        DateDTO dateDTO = null;
        if (startTime != null && endTime != null) {
            dateDTO = new DateDTO(startTime, endTime);
        }

        Page<ProjectTaskVO> pageResult;
        if (projectFilterForm.getMember() != null) {
            User user;
            try {
                user = userService.getOne(new QueryWrapper<User>().eq("name", projectFilterForm.getMember()));
            } catch (Exception e) {
                logger.error("根据用户姓名获取用户信息异常");
                throw new WorkServiceException(ResponseEnum.MYSQL_OPERATION_EXCEPTION);
            }

            if (user == null)
                return null;
            else
                pageResult = baseMapper.selectProjectTask2(user.getId(), new Page<>(pageForm.getCurrent(), pageForm.getSize()), dateDTO, projectFilterForm);
        } else
            pageResult = baseMapper.selectProjectTask1(new Page<>(pageForm.getCurrent(), pageForm.getSize()), dateDTO, projectFilterForm);

        List<ProjectTaskVO> records = pageResult.getRecords();
        if (records != null) {
            long begin = (pageForm.getCurrent() - 1) * pageForm.getSize() + 1;
            for (ProjectTaskVO record : records) {
                String rootNo = begin + "";
                record.setItemNo(rootNo);
                createItemNo(rootNo, record.getChildProjectTaskVO(), record.getTaskVOS());

                begin++;
            }
        }

        return pageResult;
    }

    @Override
    public ProjectTaskVO getProjectTaskByDirId(Integer id) {
        return baseMapper.selectProjectTask3(id);
    }

    @Override
    public Page<CommonProjectVO> getProjectRoot(Long startTime, Long endTime, PageForm pageForm) {
        DateDTO dateDTO = null;
        if (startTime != null && endTime != null) {
            dateDTO = new DateDTO(startTime, endTime);
        }

        return baseMapper.selectProjectRoot(new Page(pageForm.getCurrent(), pageForm.getSize()), dateDTO);
    }

    @Override
    public Page<CommonProjectVO> getProjectDir(Integer dirId, PageForm pageForm) {
        return baseMapper.selectProjectDir(new Page(pageForm.getCurrent(), pageForm.getSize()), dirId);
    }

    @Override
    public Page<CommonProjectVO> getProjectTask(Integer taskId, PageForm pageForm) {
        return baseMapper.selectProjectTask(new Page(pageForm.getCurrent(), pageForm.getSize()), taskId);
    }

    @Override
    public List<Right> getRights() {
        return rightService.getRightsByUserName(getUsername());
    }

    @Override
    public Page<CommonProjectVO> projectFilter(PageForm pageForm, FilterForm filterForm) {


        return null;
    }

    /**
     * 递归设置项目编号
     *
     * @param rootNo
     * @param childProjectVOS
     */
    private void createItemNo(String rootNo, List<ProjectVO> childProjectVOS) {
        int begin = 1;
        if (childProjectVOS != null)
            for (ProjectVO childProjectVO : childProjectVOS) {
                String itemNo = rootNo + "." + begin;
                childProjectVO.setItemNo(itemNo);
                if (childProjectVO.getChildProjectVO() != null)
                    createItemNo(itemNo, childProjectVO.getChildProjectVO());
                begin++;
            }
    }

    /**
     * 递归设置项目编号
     *
     * @param rootNo
     * @param childProjectTaskVOS
     */
    private void createItemNo(String rootNo, List<ProjectTaskVO> childProjectTaskVOS, List<TaskVO> childTaskVOS) {
        int begin = 1;
        if (childProjectTaskVOS != null)
            for (ProjectTaskVO childProjectTaskVO : childProjectTaskVOS) {
                String itemNo = rootNo + "." + begin;
                childProjectTaskVO.setItemNo(itemNo);
                if (childProjectTaskVO.getChildProjectTaskVO() != null)
                    createItemNo(itemNo, childProjectTaskVO.getChildProjectTaskVO(), childProjectTaskVO.getTaskVOS());
                begin++;
            }

        if (childTaskVOS != null)
            for (TaskVO childTaskVO : childTaskVOS) {
                String itemNo = rootNo + "." + begin;
                childTaskVO.setItemNo(itemNo);
                if (childTaskVO.getChildTaskVO() != null)
                    createItemNo(itemNo, null, childTaskVO.getChildTaskVO());
                begin++;
            }
    }
}

